package com.authine.cloudpivot.ext.facade.dubbo;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.engine.api.model.ResponseResult;
import com.authine.cloudpivot.engine.api.utils.ModelUtil;
import com.authine.cloudpivot.engine.plugin.domain.OpenFormType;
import com.authine.cloudpivot.engine.plugin.service.CustomerService;
import com.authine.cloudpivot.ext.model.OpenFormTypeModel;
import com.authine.cloudpivot.engine.plugin.service.OpenFormTypeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Service(
        version = "1.0"
)
@Transactional
public class CustomerFacadeImpl  implements CustomerFacade {

    private static final Logger log = LoggerFactory.getLogger(CustomerFacadeImpl.class);
    private final Object $lock = new Object[0];

    @Autowired
    OpenFormTypeService openFormTypeService;
    @Autowired
    CustomerService customerService;


    @Override
    public OpenFormTypeModel findOpenFormTypeBySchemaCodeAndCode(String scheamCode, String code) {

        log.info("开始调用查询....................");

        OpenFormType obj = openFormTypeService.findOpenFormTypeBysSchemaCodeAndCode(scheamCode, code);
        OpenFormTypeModel openFormTypeModel = ModelUtil.toModel(obj, OpenFormTypeModel.class);

        if (log.isDebugEnabled()) {
            log.debug("获取表单打开方式 数据 :{}",openFormTypeModel ==null? "null":openFormTypeModel.toString());
        }

        log.info("结束调用查询....................");
        return openFormTypeModel;
    }

    @Override
    public boolean saveOpenFormTypeBySchemaCodeAndCode(OpenFormTypeModel openFormTypeModel) {
        log.info("开始调用保存....................");
        synchronized ($lock){
            log.info("开始调用保存1....................");
            if (openFormTypeModel == null) {
                log.debug("数据对象不能为空");
                return false;
            }else if(StringUtils.isEmpty(openFormTypeModel.getSchemaCode()) || StringUtils.isEmpty(openFormTypeModel.getCode()) ||openFormTypeModel.getType() == null){
                log.debug("schemaCode ,code ,type不能为空");
                return false;
            }else{

                OpenFormType obj = openFormTypeService.findOpenFormTypeBysSchemaCodeAndCode(openFormTypeModel.getSchemaCode(), openFormTypeModel.getCode());

                if (obj != null) {
                    obj.setCode(openFormTypeModel.getCode());
                    obj.setSchemaCode(openFormTypeModel.getSchemaCode());
                    obj.setType(openFormTypeModel.getType());
                    obj.setCreatedBy(openFormTypeModel.getModifiedBy());
                    obj.setModifiedTime(openFormTypeModel.getModifiedTime());
                }else{
                    obj = ModelUtil.toEntity(openFormTypeModel, OpenFormType.class);
                }
                openFormTypeService.saveOpenFormType(obj);
                log.info("结束调用保存....................");
                return  true;
            }


        }

    }

    @Override
    public ResponseResult queryByMap(Map<String, Object> map) {
        log.info("开始调用getListByMap....................");
        String s = JSON.toJSONString(map);

        String[] ss = {"delete","DELETE","dorp","DORP","update","UPDATE"};

        for (String sss: ss ) {
            if (StringUtils.indexOfIgnoreCase(s,sss) !=-1){
               return ResponseResult.builder().errcode(-1).errmsg("sql 不支持 delete,update, dorp").build();
            }
        }

        return ResponseResult.builder().errcode(0).data(customerService.getListByMap(map)).build();
    }
}
